﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DBAccess;

namespace Project
{
    internal class Projects : iProjects
    {
        public Projects()
        { }

        public DataTable GetAllProjects()
        {
            string sql = "SELECT id_project,name, id_user, cast (startdate as char) as startdate,  cast (enddate as char) as enddate  FROM Project";         
            DataTable dt = new DataTable();
            dt = BuildIExecute.Build().ExecuteQuery(sql);

            //foreach (DataRow row in dt.Rows)
            //{
            //    if (row["id_user"].ToString() == "0")
            //        row["id_user"] = " Иванов ";
            //}
      
            // заменяем подписи столбов
            dt.Columns["id_project"].ColumnName = "#";
            dt.Columns["name"].ColumnName = "Наименование";
            dt.Columns["id_user"].ColumnName = "Менеджер проекта";
            dt.Columns["startdate"].ColumnName = "Дата начала";            
            dt.Columns["enddate"].ColumnName = "Дата окончания";           
          
            return dt;
        }

        public void AddProject(string ProjectName, int ProjectManagerId, string ProjetStartDate, string ProjectEndDate)
        {
            Project p = new Project(ProjectName,ProjectManagerId, ProjetStartDate, ProjectEndDate);
            string sql = String.Format("INSERT INTO Project (name,id_user, startdate, enddate) VALUES ('{0}', '{1}' , CAST ('{2}' as date) , CAST  ('{3}' as date))", p.Name,p.ManagerId, p.StartDate, p.EndDate);
            BuildIExecute.Build().ExecuteNonQuery(sql);
        }

        public void DeleteProject(string id_project)
        {            
            string sql = String.Format("DELETE FROM Project WHERE id_project = '{0}'", id_project);
            BuildIExecute.Build().ExecuteNonQuery(sql);
        }

    }
}
